package account.action;

import account.entity.AccountVO;
import account.service.AccountService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import util.ObtainOther;

import java.util.List;

public class AccountAction extends ActionSupport {

    private final static Log logger = LogFactory.getLog(AccountAction.class);

    private AccountVO accountVO;

    public AccountVO getAccountVO() {
        return accountVO;
    }

    public void setAccountVO(AccountVO accountVO) {

        this.accountVO = accountVO;
    }

    private List<AccountVO> accountVOs;

    public List<AccountVO> getAccountVOs() {
        return accountVOs;
    }

    public void setAccountVOs(List<AccountVO> accountVOs) {
        this.accountVOs = accountVOs;
    }

    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    private AccountService accountService;

    public AccountService getAccountService() {
        return accountService;
    }

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 删除该用户帐号
     *
     * @return
     */
    public String deleteAccount() {
        try {
            accountService.deleteAccountById(id);
            message=ObtainOther.obtainMessage("deleteAccountSuccess");
            return SUCCESS;

        } catch (Exception e) {
            message=ObtainOther.obtainMessage("deleteAccountError");
            logger.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 读取单个用户帐号
     *
     * @return
     */
    public String loadAccount() {


        try {
            this.accountVO = accountService.findAccountVoById(id);
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("loadAccountError");
            logger.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 更新用户帐号的资料
     *
     * @return
     */
    public String updateAccount() {
        try {
            accountVO=accountService.updateAccount(accountVO);
            message=ObtainOther.obtainMessage("updateAccountSuccess");
            return SUCCESS;
        } catch (Exception e) {
            message=ObtainOther.obtainMessage("updateAccountError");
            logger.error(message + ":/n" + e);
            return ERROR;
        }
    }

}
